Skip to content

trace: add max_error to reject fused double-order clusters#37

Closed
nikolai-piskunov wants to merge 1 commit into
ivh:masterfrom
nikolai-piskunov:max-error-trace-rejection
Closed

trace: add max_error to reject fused double-order clusters#37
nikolai-piskunov wants to merge 1 commit into
ivh:masterfrom
nikolai-piskunov:max-error-trace-rejection

Conversation

@nikolai-piskunov

Copy link
Copy Markdown
Contributor

Summary

Adds an optional max_error parameter to trace(). After the final polynomial fit, any cluster whose residual RMS (in pixels) exceeds max_error is discarded.

The motivation is closely-packed detectors where two adjacent spectral orders can be merged into a single cluster (typically at detector edges, where the gap between orders closes up). Such a fused cluster cannot be followed by a single polynomial, so its fit RMS is large — on the order of half the order separation — whereas a genuine single-order trace fits to sub-pixel RMS. Thresholding on the fit RMS cleanly rejects the fused clusters while leaving real orders untouched.

This ports the MAX_ERR keyword from the IDL REDUCE mark_orders, where it was added for exactly this case (e.g. MOSAIC NIR, ~5 px median order spacing).

It is disabled by default (max_error=None), so existing reductions are unaffected.

Changes

  • trace.trace(): new max_error=None parameter; reject clusters with fit RMS above it (logged when any are dropped).
  • reduce.py Trace step: read max_error from config (defaults to None) and pass it through.
  • instruments/defaults/schema.json and defaults/settings.json: add max_error (default null).
  • test/test_trace.py: TestMaxError — disabled / generous / tiny thresholds, plus a fused high-RMS cluster being dropped while a clean order survives.

Notes

While porting I checked the other order-tracing improvements from the IDL side and found PyReduce already covers them: the 90 % overlap auto-merge guard (auto_merge_threshold), degree-2 fitting before merge (degree_before_merge), and gap handling in determine_overlap_rating. numpy's SVD-based lstsq is also robust to the polynomial conditioning that needed an explicit basis rescale in IDL — verified no RankWarning and identical coefficients at degree 4 over 4096 columns. So max_error is the one genuinely new piece.

Testing

pytest test/test_trace.py and test/test_configuration.py pass (66 trace tests including the 4 new ones; config tests confirm the default settings still validate against the schema).

🤖 Generated with Claude Code

Adds an optional `max_error` parameter to `trace()`: after the final
polynomial fit, clusters whose residual RMS (in pixels) exceeds the
threshold are discarded. A cluster that accidentally spans two separate
orders cannot be followed by a single polynomial, so its RMS is large
(~half the order separation) compared to a genuine trace (sub-pixel);
this rejects such fused clusters.

Ports the MAX_ERR keyword from the IDL REDUCE mark_orders, where it was
introduced to handle closely-packed detectors (e.g. MOSAIC NIR) on which
adjacent orders can merge into a single cluster at detector edges.

- trace.trace(): new `max_error=None` parameter (disabled by default)
- reduce.py Trace step: read `max_error` from config and pass through
- schema.json / default settings.json: add `max_error` (default null)
- tests: TestMaxError covering disabled, generous, tiny, and a fused
  high-RMS cluster being dropped while a clean order survives

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@codecov-commenter

This comment was marked as spam.

@ivh

ivh commented Jun 25, 2026

Copy link
Copy Markdown
Owner

Merged into master via rebase (so GitHub doesn't auto-mark this as merged). Landed as commits cf81ab4 (the max_error feature) and 977a0cb (added fused-order + config plumbing tests). Both are on origin/master.

@ivh ivh closed this Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants